package me.sdevil507.mobile.wxApplet.service.impl;

import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import me.sdevil507.mobile.wxApplet.dto.BwechatCheckUserDTO;
import me.sdevil507.mobile.wxApplet.entity.BwechatCheckUserPO;
import me.sdevil507.mobile.wxApplet.entity.QBwechatCheckUserPO;
import me.sdevil507.mobile.wxApplet.model.WxBO;
import me.sdevil507.mobile.wxApplet.repository.BwechatCheckUserRepository;
import me.sdevil507.mobile.wxApplet.service.WxCheckUserService;
import me.sdevil507.org.po.QOrgUserRelPo;
import me.sdevil507.supports.jpa.PageParamsDTO;
import me.sdevil507.supports.jpa.SortParamsDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class WxCheckUserServiceImpl implements WxCheckUserService {

    @Autowired
    BwechatCheckUserRepository bwechatCheckUserRepository;

    @Override
    public void saveWxCheckUserData(String name, String phoneNumber, Long orgId, String status) {
        //查询是否存在待审核
        BwechatCheckUserPO bwechatCheckUserPO = bwechatCheckUserRepository.findByNameAndPhoneAndOrgIdAndStatus(name, phoneNumber, orgId, status);
        if(bwechatCheckUserPO == null){
            bwechatCheckUserPO = new BwechatCheckUserPO();
            bwechatCheckUserPO.setName(name);
            bwechatCheckUserPO.setPhone(phoneNumber);
            bwechatCheckUserPO.setOrgId(orgId);
            bwechatCheckUserPO.setStatus(status);
            bwechatCheckUserRepository.save(bwechatCheckUserPO);
        }
    }

    @Override
    public Page<BwechatCheckUserPO> readAllByPage(BwechatCheckUserDTO searchDTO, PageParamsDTO pageParamsDto, SortParamsDTO sortParamsDto) {

        QBwechatCheckUserPO qBwechatCheckUserPO = QBwechatCheckUserPO.bwechatCheckUserPO;

        // 开启谓语查询条件
        Predicate predicate = qBwechatCheckUserPO.id.isNotNull();

        String name = searchDTO.getRealName();
        if (!StringUtils.isEmpty(name)) {
            predicate = ExpressionUtils.and(predicate, qBwechatCheckUserPO.name.eq(name));
        }

        String phone = searchDTO.getPhoneNumber();
        if (!StringUtils.isEmpty(phone)) {
            predicate = ExpressionUtils.and(predicate, qBwechatCheckUserPO.phone.eq(phone));
        }

        String status = searchDTO.getStatus();
        if (!StringUtils.isEmpty(status)) {
            predicate = ExpressionUtils.and(predicate, qBwechatCheckUserPO.status.eq(status));
        }

        //如果是机构端查询
        Long orgId = searchDTO.getOrgId();
        if (null != orgId && orgId.longValue() > 0) {
            ExpressionUtils.and(predicate, qBwechatCheckUserPO.orgId.eq(orgId));
        }

        Sort sort;
        if (null == sortParamsDto) {
            sort = Sort.by(Sort.Direction.DESC, "createTime");
        } else {
            // 排序
            sort = sortParamsDto.getSort();
            if (null == sort) {
                sort = Sort.by(Sort.Direction.DESC, "createTime");
            }
        }

        // 执行返回
        Pageable pageable = pageParamsDto.getPageableWithSort(sort);

        return bwechatCheckUserRepository.findAll(predicate, pageable);
    }

    @Override
    public void updateCheckUserData(BwechatCheckUserDTO dto) {
        BwechatCheckUserPO bwechatCheckUserPO = bwechatCheckUserRepository.getOne(dto.getId());
        bwechatCheckUserPO.setStatus(dto.getStatus());
        bwechatCheckUserRepository.save(bwechatCheckUserPO);
    }
}
